/* $Header: readiness.c,v 2.2 01/10/02 10:33:15 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  readiness.c                                           */
/* ============                                                        */
/*                                                                     */
/* Author: Lesley Freed                                                */
/*                                                                     */
/* MODULE PURPOSE:  To evaluate and report readiness.                  */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <zone_status.h>
#include <status.h>
#include <i_op.h>
#include <leds.h>
#include <iomap.h>
#include <error.h>
#include <run_ui.h>

#include <exception.h>
#include <err_handler.h>

#include <readiness.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/

static READY_STATE HoldOffReady( void );



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

/***********************************************************************/
/* FUNCTION:  ReadyHoldOffEval                                         */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To return the readiness state of the flow, thermal and    */
/*           detector electronics control after all of their readiness */
/*           states have been evaluated.                               */
/*                                                                     */
/* RETURNED VALUE:  READY_STATE:  READY = ready, NOT_READY = not ready,*/
/*                  DONT_KNOW = not yet evaluated                      */
/*                                                                     */
/* GLOBALS USED: none                                                  */
/*                                                                     */
/* CALLS:  TestForNotReadyCoreEval, TestForNotReadyDetEval             */
/*                                                                     */
/* LIMITATIONS:  You MUST call ClearNotReadyEval BEFORE this to guar-  */
/*               antee a new readiness evaluation.                     */
/*                                                                     */
/***********************************************************************/

READY_STATE ReadyHoldOffEval(void)
{
   if ( HoldOffTestForNotReadyCoreEval() && TestForNotReadyDetEval() )
   {
      return HoldOffReady();
   }
   else
   {
      return DONT_KNOW;
   }
}

/***********************************************************************/
/* FUNCTION:  HoldOffReady                                             */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To return the readiness of the flow, thermal and detector */
/*           electronics control.                                      */
/*                                                                     */
/* RETURNED VALUE: READY_STATE:  READY = ready, NOT_READY = not ready  */
/*                                                                     */
/***********************************************************************/

static READY_STATE HoldOffReady( void )
{
   if ( TestForNonFatalErr() || TestForNotReadyDet() || HoldOffTestForNotReadyCore() )
   {
      return NOT_READY;
   }
   else
   {
      return READY;
   }
}

/***********************************************************************/
/* FUNCTION:  ReadyEval                                                */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To return the readiness state of the system after all of  */
/*           the appropriate states have been evaluated.               */
/*                                                                     */
/* RETURNED VALUE:  READY_STATE:  READY = ready, FALSE = not ready,    */
/*                  DONT_KNOW = not yet evaluated                      */
/*                                                                     */
/* GLOBALS USED: none                                                  */
/*                                                                     */
/* CALLS:  TestForNotReadyCoreEval, TestForNotReadyDetEval             */
/*         TestForNotReadyEval                                         */
/*                                                                     */
/* LIMITATIONS:  You MUST call ClearNotReadyEval BEFORE this to get    */
/*               the updated readiness state.                          */
/*                                                                     */
/***********************************************************************/

READY_STATE ReadyEval (void)
{
   /* See if the repeatitively evaluated readiesses are evaluated. */
   /* Note that we will not check NotReadyExt, this is not updated */
   /* repeatitively.                                               */
   if ( TestForNotReadyCoreEval() && TestForNotReadyDetEval() &&
        TestForNotReadyEval() )
   {
      return Ready();
   }
   else
   {
      return DONT_KNOW;
   }
}

/***********************************************************************/
/* FUNCTION:  Ready                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To return the readiness of the system since it was last   */
/*           evaluated.                                                */
/*                                                                     */
/* RETURNED VALUE: READY_STATE:  READY = ready, NOT_READY = not ready  */
/*                                                                     */
/* LIMITATIONS:  This will NOT be guaranteed to be correct for the     */
/*               exact instant that it is called.  Readiness is eval-  */
/*               uated on a periodic basis.  This function only        */
/*               reflects the readiness stated of the instrument since */
/*               it was last evaluated.  Use ClearNotReadyEval and     */
/*               then ReadyEval to get the exact readiness state of    */
/*               the system.                                           */
/*                                                                     */
/***********************************************************************/

READY_STATE Ready( void )
{
   if ( ( InternalReady() == READY ) &&
        ( ExternalReady() == READY ) &&
        ( HostReady()     == READY ) )
   {
      return READY;
   }
   else
   {
      return NOT_READY;
   }
}

/* GCReady will ignore External Readiness during a run since we are by  */
/* definition not ready externally when we are in a run.                */

READY_STATE GCReady( void )
{
   if ( ( Ready() == READY ) ||
        ( ( InternalReady() == READY ) && RunActiveOrPostTime() ) )
   {
      return READY;
   }
   else
   {
      return NOT_READY;
   }
}


READY_STATE InternalReadyForPrepRunEval( void )
{
   if ( TestForNotReadyCoreEval() && TestForNotReadyDetEval() &&
        TestForNotReadyEval() )
   {
      return InternalReadyForPrepRun();
   }
   else
   {
      return DONT_KNOW;
   }
}

READY_STATE InternalReadyForPrepRun( void )
{
   /* See if the whole machine is ready besides the not ready */
   /* conditions that require pre-run                         */

   if ( TestForNonFatalErr() ||
        TestForNotReadyPreRun() ||
        TestForNotReadyCore() ||
        TestForNotReadyDet() )
   {
      return NOT_READY;
   }

   return READY;
}

READY_STATE InternalReadyEval( void )
{
   if ( TestForNotReadyCoreEval() &&
        TestForNotReadyDetEval()  &&
        TestForNotReadyEval() )
   {
      return InternalReady();
   }
   else
   {
      return DONT_KNOW;
   }
}

READY_STATE InternalReady( void )
{
   if ( TestForNonFatalErr() ||
        TestForNotReadyCore() || TestForNotReadyDet() || TestForNotReady() )
   {
      return NOT_READY;
   }
   else
   {
      return READY;
   }
}

READY_STATE ExternalReady( void )
{
   if ( TestForException( EXTERNAL_DEVICE ) )
   {
      return NOT_READY;
   }
   else
   {
      return READY;
   }
}


READY_STATE HostReady(void)
{
   if ( TestForException( HOST ) )
   {
      return NOT_READY;
   }
   else
   {
      return READY;
   }
}

void SetHostReady( INT32 ready )
{
   if ( ready )
   {
      ClearNewException( HOST );
   }
   else
   {
      SetNewException( HOST, 0, 0 );
   }

}


/* Return whether or not an external device is not ready.  Since the NOT */
/* READY line is wire-ored we will return FALSE if we are not ready.     */

BIT8 ExternalDeviceNotReady( void )
{
   if ( ( InternalReady() == READY ) &&
        ( HostReady() == READY ) )
   {
      if ( TestForException( EXTERNAL_DEVICE ) )
      {
         return TRUE;
      }
      else
      {
         return FALSE;
      }
   }
   else
   {
      return FALSE;
   }
}


/***********************************************************************/
/* FUNCTION: SetNotReadyLedState                                       */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To check the non-fatal error and readiness bits and set   */
/*           the not-ready LED accordingly.                            */
/*                                                                     */
/* RETURNED VALUE:  none                                               */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  Ready, TestForNonFatalErr                                   */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void SetNotReadyLedState( void )
{
   /*****************************************************************/
   /*                                                               */
   /*  For the GC to be "READY" all applicable small zones,         */
   /*  pneumatic controls and the oven must be ready and there      */
   /*  must be no non-fatal errors active.  The following           */
   /*  definitions are used to determine readiness:                 */
   /*                                                               */
   /*  Readiness is for the small thermal zones and the pneumatics  */
   /*  is defined as:                                               */
   /*                                                               */
   /*  1)  zone installed, ON and in control                        */
   /*  -OR-                                                         */
   /*  2)  zone installed and OFF                                   */
   /*                                                               */
   /*  Readiness for the oven is defined as:                        */
   /*     oven ON and in control                                    */
   /*                                                               */
   /*****************************************************************/

   /* If we have any non-fatal errors then there is a problem with the */
   /* instrument and we cannot guarantee successful operation.  We will*/
   /* flag this by BLINKING the Not-Ready LED.                         */

   if ( TestForNonFatalErr() )
   {
//         BLINK_NOT_READY_LED();                      /* Not Ready */
   }
   else
   {
      /* We will ignore external readiness if we are in a run */
      if ( GCReady() == READY )
      {
//         TURN_NOT_READY_LED_OFF();                   /* Ready     */
      }
      else
      {
//         TURN_NOT_READY_LED_ON();                    /* Not Ready */
      }

   }
}

